<?php

	/**
	 * Prevent direct execution of the bootstrap script
	 */
	(sizeof(get_included_files()) != 1) or exit;
	
	/**
	 * General constant needed to access include files,
	 * also acts as an initial load time reference
	 */
	define('ENGINE', microtime(true));
	
	/**
	 * Disable html errors, so error messages dont link to the 
	 * php.net manual
	 */
	ini_set('html_errors', 'Off');
	
	/**
	 * Disable magic runtime quotes, this helps not cluttering 
	 * up alot of the code to check for them, simply just by 
	 * disabling them
	 */
	ini_set('magic_quotes_gpc', 'Off');
	
	/**
	 * Sets new constants regarding PHP_VERSION and PHP_VERSION_ID constants,
	 * depending on how PHP may interperet them.  This allows for consistent
	 * constant calls, and  determines them into the framework.
	 */
	$version = (string) ((defined('PHP_VERSION')) ? PHP_VERSION : phpversion());
																   
	define('ENGINE_PHP_VERSION', $version);
	define('ENGINE_PHP_VERSION_ID', ((defined('PHP_VERSION_ID')) ? (int) PHP_VERSION_ID : (int) ($version{2} * 10000 + $version{2} * 100 + $version{4})));
	
	unset($version);
	
	/**
	 * Verify the server is using at least the lowest compatability version
	 * of PHP on their server
	 */
	if(version_compare(ENGINE_PHP_VERSION, '5.3.0') < 0)
	{
		die('Your server is running an incompatable version of PHP (' . ENGINE_PHP_VERSION . ').  You require at least 5.3.0 to execute this environment.');
	}
	
	/**
	 * Determines the Server Software and Version into a more
	 * easily accessable constant
	 */
	define('ENGINE_SERVER_VERSION', (string) str_replace(' PHP ' . PHP_VERSION, '', str_replace('/', ' ', $_SERVER['SERVER_SOFTWARE'])));
	
	/**
	 * Set error reporting level
	 */
	error_reporting(-1);
	
	/**
	 * Engine directory reference
	 */
	define('ENGINE_DIR', (string) dirname(__FILE__));
	
	/**
	 * URL of the current page being executed, including its 
	 * query string, note that this constant is using the 
	 * raw data. It is up to the user of this constant to 
	 * proper filter it.
	 */
	define('ENGINE_SELF', (substr(strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos($_SERVER['SERVER_PROTOCOL'], '/'))) . ((empty($_SERVER['HTTPS'])) ? '://' : 's://') . $_SERVER['SERVER_NAME'] . (($_SERVER['SERVER_PORT'] == '80') ? false : ':' . $_SERVER['SERVER_PORT']) . $_SERVER['SCRIPT_NAME'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : ''));
	
	/**
	 * Set default timezone
	 */
	date_default_timezone_set('UTC');
	
	/**
	 * Basic time definitions for functions
	 */
	define('SECOND', 1);
	define('MINUTE', 60 * SECOND);
	define('HOUR', 60 * MINUTE);
	define('DAY', 24 * HOUR);
	define('WEEK', 7 * DAY);
	define('MONTH', 30 * DAY);
	define('YEAR', 365 * DAY);
	
	/**
	 * Include configuratons
	 */
	require(ENGINE_DIR . '/configuration.php');
	
	/**
	 * Load internal functionality
	 */
	if(is_dir(ENGINE_DIR . DIRECTORY_SEPARATOR . 'functions'))
	{
		foreach(glob(ENGINE_DIR . DIRECTORY_SEPARATOR . 'functions' . DIRECTORY_SEPARATOR . '*') as $file)
		{
			if(substr($file, strrpos($file, '.')) != '.php')
			{
				continue;	
			}
			
			require($file);
		}
		
		unset($file);
	}
	
	/**
	 * Set various handlers for errors, exceptions, 
	 * shutdown and autoloading
	 */
	set_error_handler('engine_error_handler');
	set_exception_handler('engine_exception_handler');
	register_shutdown_function('engine_shutdown_handler');
	spl_autoload_register('engine_autoload_handler');
	
	/**
	 * Data filter constant, numeric value
	 */
	define('TYPE_NUMERIC', 0x0001);
	
	/**
	 * Data filter constant, string value
	 */
	define('TYPE_STRING', 0x0002);
	
	/**
	 * Data filter constant, email value
	 */
	define('TYPE_EMAIL', 0x0003);
	
	/**
	 * Data filter constant, boolean value
	 */
	define('TYPE_BOOLEAN', 0x0004);
	
	/**
	 * Data filter constant, callback value
	 */
	define('TYPE_CALLBACK', 0x0005);
	
	/**
	 * Data filter option, gets the raw value
	 * of the input without any type of santizing
	 */
	define('INPUT_OPT_RAW', 0x01FF);
	
	/**
	 * Data filter option, tells the cleaner that this
	 * is an array input and any of its elements must be of
	 * the given type.  Note that recursive operations are not
	 * done by the data filter
	 */
	define('INPUT_OPT_ARRAY', 0x02FF);
	
	/**
	 * Construct the main registry
	 */
	$engine = Engine::init($configuration);
	
	/**
	 * Set globals
	 */
	Engine::globals('error_reporting', (bool) true);
	Engine::globals('errors', (object) new ArrayObject);
	
	/**
	 * Set database table prefix constant
	 */
	define('PREFIX', (string) $configuration['database']['prefix']);
	
	/**
	 * Set the debug mode constant
	 */
	define('DEBUG', (bool) Engine::DEBUG);
	
	/**
	 * Register the default instances
	 */
	$engine->register('db', 'Database');
	$engine->register('cache', 'Datastore');
	$engine->register('filter', 'Datafilter');
	
	/**
	 * Precache elements from the datastore
	 */
	$cache_buffer		= array();
	$default_precache	= array('options', 'modules');
	
	$cache->cache((!isset($precache) ? $default_precache : false), $cache_buffer) or engine_multi_error('Unable to load template \'%s\', datastore possibly corrupted!', $cache_buffer);
	
	unset($default_buffer, $cache_buffer);
	
	/**
	 * Enables to use of the modular system
	 * and permanent session storage
	 */
	$engine->register('session', 'Sessions');
	$engine->register('module', 'Modules');
	$engine->register('template', 'Template');
	
	/**
	 * Dynamically generated content should not be cached
	 * by browsers or any proxy caches. This forces proxies
	 * and browsers to disable their caching for this site,
	 * when using HTTP/1.0 and HTTP/1.1
	 */
	header('Expires: Sat, 26 Jul 1997 05:00:00 GMT' ); 
	header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 
	header('Cache-Control: no-store, no-cache, must-revalidate'); 
	header('Cache-Control: post-check=0, pre-check=0', false); 
	header('Pragma: no-cache' ); 
	
	/**
	 * Miscellanious HTTP Reponse Headers
	 */
	header('X-Powered-By: Chase Softwware ' . Engine::VERSION_STRING);
	header('X-Status: ' . (DEBUG) ? 'Debug Mode' : 'Production Mode');
	
?>